package model;

import java.util.ArrayList;
import java.util.List;

import controller.FileDoc;
/**
 * This class represents an address book
 * We have a List of contact, the name of the owner, and the list of the groups which exist in the address book
 * @author Estelle
 *
 */
public class Carnet {
	private String name;
	private List<Contact> content;
	private List<String> groupContact;
	
//--------------------------------------------------------------------------------------------------------------------------------------------------------
	/**
	 * contructor which takes the string of the owner's name
	 * @param name
	 */
	public Carnet(String name){
		this.name = name;
		this.content = new ArrayList<Contact>();
		this.groupContact = new ArrayList<String>();
		
	}
	
//--------------------------------------------------------------------------------------------------------------------------------------------------------
	/**
	 * to add a new contact to the address book
	 * @param newContact
	 */
	public void addNewContact(Contact newContact){
		this.content.add(newContact);	
		updateGroupeAdd();
	}
	
//--------------------------------------------------------------------------------------------------------------------------------------------------------
	/**
	 * to update the list of the group after an add of a contact
	 */
	public void updateGroupeAdd(){
	
		for( Contact con : content){
				if(!groupContact.contains(con.getGroupe().toLowerCase()) && !con.getGroupe().equals("groupe")){
					groupContact.add(con.getGroupe().toLowerCase());
					
				}	
		}
		
	}
	
//--------------------------------------------------------------------------------------------------------------------------------------------------------
	/**
	 * to update the list of group after the delete of a contact
	 */
	public void updateGroupeDelete(){
		
		boolean flag = false;	
		List<String> buff = groupContact;
		if( buff.isEmpty()) System.out.println("l49 carnet : buff is empty");
			
		for( String grp : buff){
			flag = false;
			int i =0;
			System.out.println("\n\nl54 carnet : --- START ----" + i + " groupe dans groupContact " + grp);
			for(Contact con : content){
				if( grp.equals(con.getGroupe())){
					System.out.println("\n______l55 carnet : *grp dans groupContact :" + grp + " == contact pour le contact : " + con.getGroupe() + "_____ \n");
					flag = true;
					break;
				}
				
				i++;
			}	
			System.out.println("l60 carnet : --- END ----" + i + " flag = " + flag );
			
			if(!flag){
				System.out.println("l62 carnet : flag = false : grp n'est pas dans les contacts :" + grp);
				groupContact.remove(grp);
			}	
		}	
	}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
	/**
	 * to search a contact in the address book thank a key word
	 * @param firstName
	 * @return
	 */
	public Contact searchContact(String firstName){
		Contact found = new Contact();
		
		if(this.content.isEmpty()) {
			System.out.println("error l46 in carnet function searchcontact: le  carnet est vide ");
			return found;
		}
		for(Contact friend : this.content){
			if(firstName.toLowerCase().contains(friend.getFirstName().toLowerCase()) || firstName.toLowerCase().contains(friend.getLastName().toLowerCase()) ){
				found.copyContact(friend);
				break;
			}
			
		}
		
		return found;
	}
	
//--------------------------------------------------------------------------------------------------------------------------------------------------------
	/**
	 * to get the list of contact which is in a group thank to the name of the group
	 * @param groupe
	 * @return
	 */
	public List<Contact> searchContactGroup(String groupe){
		List<Contact> found = new ArrayList<Contact>();
		if(this.content.isEmpty()) {
			return found;
		}
		for(Contact friend : this.content){
			if(friend.getGroupe().toUpperCase().equals(groupe.toUpperCase())){
				found.add(friend);
			}
		}
		
		return found;
	}
	
//--------------------------------------------------------------------------------------------------------------------------------------------------------
	/**
	 * to find and get the id of a contact
	 * @param contact
	 * @return
	 */
	public int getIDContact(Contact contact){
		
		int i =0;
		for(Contact friend : this.content){
			if( friend.isEqual(contact)){
				return i;
			}
			i++;	
		}
		return -1;
		
	}
	
//--------------------------------------------------------------------------------------------------------------------------------------------------------
	/**
	 * to reset an address book and to transform it in another address book sent as parameter
	 * @param newCarnet
	 */
	public void copy(Carnet newCarnet){
		FileDoc file = new FileDoc();
		this.name = newCarnet.getName();
		this.content = newCarnet.getContent();
		this.groupContact = newCarnet.getGroupContact();
		file.resetCarnet(this);
	}

//--------------------------------------------------------------------------------------------------------------------------------------------------------
	/**
	 * getter and setter of the class	
	 * @return
	 */
	public List<String> getGroupContact() {
		return groupContact;
	}
		
	public void setGroupContact(List<String> groupContact) {
		this.groupContact = groupContact;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Contact> getContent() {
		return content;
	}

	public void setContent(List<Contact> content) {
		this.content = content;
	}
	
//--------------------------------------------------------------------------------------------------------------------------------------------------------
	/**
	 * to print the content of the address book in the console
	 */
	public void printCarnet(){
		System.out.println("The owner is :"+ this.name);
		StringBuilder buff = new StringBuilder();
		for( Contact con : this.content){
			System.out.println("Name :"+ con.getFirstName() + " " + con.getLastName());
			System.out.println("\t HomeAddress :" + con.getHomeAddress());
			System.out.println("\t CompanyAddress :" + con.getCompanyAddress());
			System.out.println("\t HomePage :" + con.getHomePage());
			System.out.println("\t email :" + con.getEmail());
			System.out.println("\t personal numbers :" + con.getPersonalPhone());
			System.out.println("\t groupe:" + con.getGroupe());
			
			for(Integer num : con.getDate()){
				buff.append(num).append("/");
			}
			System.out.println("\t date:" + buff.toString());
			buff.setLength(0);
			
		}
	}
	
//--------------------------------------------------------------------------------------------------------------------------------------------------------
	/**
	 * to print the list of the group
	 */
	public void printGroupContact(){
		System.out.println("The owner is :"+ this.name);
		for( String grp : this.groupContact){
			System.out.println(" - "+ grp);
			
		}
	}
	
}

